package com.h3bpm.handler.Utils;

import com.h3bpm.handler.domin.EsbReqHead;
import com.h3bpm.handler.domin.EsbResHead;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.nio.charset.Charset;

/**
 * 自定义http类
 *
 */
public class HttpLocalUtils {

    private static Logger logger = Logger.getLogger(HttpLocalUtils.class);

    /**
     * ESB对接请求接口
     * @param url
     * @param json
     * @return
     */
    private static CloseableHttpClient httpClient = null;

    public static String doPostToESB(String url, String json, EsbReqHead esbReqHead) {
        logger.info("请求ESB信息,url:"+url+", sourceSystem："+esbReqHead.get(EsbReqHead.source_System)
                +", targetSystem："+esbReqHead.get(EsbReqHead.target_System)
                +", requestId："+esbReqHead.get(EsbReqHead.request_Id)
                +", serviceName："+esbReqHead.get(EsbReqHead.service_Name)
                +", json："+json);
        try {
            httpClient = HttpClientBuilder.create().build();
//            RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(5000)
//                    .setSocketTimeout(2000).setConnectTimeout(2000).build();
            HttpPost post = new HttpPost(url);
//            post.setConfig(requestConfig);
            post.addHeader(EsbReqHead.source_System, (String)esbReqHead.get(EsbReqHead.source_System));
            post.addHeader(EsbReqHead.target_System, (String)esbReqHead.get(EsbReqHead.target_System));
            post.addHeader(EsbReqHead.request_Id, (String)esbReqHead.get(EsbReqHead.request_Id));
            post.addHeader(EsbReqHead.service_Name, (String)esbReqHead.get(EsbReqHead.service_Name));
            //post.addHeader("Authorization", authorization);
            StringEntity s = new StringEntity(json, Charset.forName("UTF-8"));
            s.setContentEncoding("UTF-8");
            //发送json数据需要设置contentType
            //设置请求参数
            post.setEntity(s);
            HttpResponse response = httpClient.execute(post);
            org.apache.http.Header[] statusFlags = response.getHeaders(EsbResHead.status_Flag);
            if (statusFlags[0].getValue().equals("1")) {
                //返回json格式
                String res = EntityUtils.toString(response.getEntity());
                return res;
            }
            if (statusFlags[0].getValue().equals("0")) {
                //返回json格式
                String res = EntityUtils.toString(response.getEntity());
                logger.error(String.format("请求失败url：%s,结果res", url, res));
                return res;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (httpClient != null) {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}


